package com.tvunetworks.center.user.controller;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.constant.LocalConstant;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.PageOverWatchParam;
import com.tvunetworks.center.user.model.param.UpdateOverWatchParam;
import com.tvunetworks.center.user.model.vo.OverWatchVo;
import com.tvunetworks.center.user.service.OverWatchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Etan Yang
 * @data: 6/17/2020 9:46 AM
 * @program: tvucc-aggregator
 * @description:
 */

@Slf4j
@RestController
@RequestMapping("/overWatch")
public class OverWatchController {
    @Autowired
    private OverWatchService overWatchService;

    /**
     * 分页查询该用户的T/R
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("page")
    public Result<PageInfo> pageDeviceRByUser(@Logined LoginUser loginUser,@RequestBody PageOverWatchParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        String role = loginUser.getRole();
        if (StringUtils.isEmpty(loginUser.getRole()) || !LocalConstant.UserRole.ADMIN.equalsIgnoreCase(role)) {
            log.error("user role is not admin, loginUser:[{}]", loginUser);
            return ResultUtil.noAuthority();
        }
        return ResultUtil.success(overWatchService.pageDeviceRByUser(param,loginUser.getId()));
    }

    /**
     * 修改指定R的code码和最大访问数
     * @param param param
     * @param loginUser loginUser
     * @return Result
     */
    @PostMapping("update")
    public Result<Object> updateOverWatch(@Logined LoginUser loginUser, @RequestBody @Valid UpdateOverWatchParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        String role = loginUser.getRole();
        if (StringUtils.isEmpty(loginUser.getRole()) || !LocalConstant.UserRole.ADMIN.equalsIgnoreCase(role)) {
            return ResultUtil.noAuthority();
        }
        int result = overWatchService.updateOverWatch(param);
        if (result == 1) {
            return ResultUtil.error("81002001", "rid and tid can not empty at the same time");
        }
        if (result == 2) {
            return ResultUtil.error("81002002", "the number can not lt or eq 0");
        }
        if (result == 3) {
            return ResultUtil.error("81002003", "the code length must be 6");
        }
        if (result == 4) {
            return ResultUtil.error("81002004", "over Watch not exist");
        }
        if (result == 5) {
            return ResultUtil.error("81002005", "this code already exist");
        }
        return ResultUtil.success();
    }

    /**
     * 登录指定的code码观看对应的视频
     * @param code code
     * @return Result
     */
    @PostMapping("login")
    public Result<OverWatchVo> loginOverWatch(@RequestParam String code) {
        return overWatchService.loginOverWatch(code);
    }

    /**
     * 校验code是否存在
     * @param code code
     * @param loginUser loginUser
     * @return Result
     */
    @PostMapping("checkCode")
    public Result<Object> checkCode(@Logined LoginUser loginUser, @RequestParam String code) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        String role = loginUser.getRole();
        if (StringUtils.isEmpty(loginUser.getRole()) || !LocalConstant.UserRole.ADMIN.equalsIgnoreCase(role)) {
            return ResultUtil.noAuthority();
        }
        int result = overWatchService.checkCode(code);
        if (result == 1) {
            return ResultUtil.error("81002005", "this code exist");
        }
        return ResultUtil.success();
    }


    /**
     * 通过rid和code校验该R的code码是否发生改变
     * @param rid rid
     * @param code code
     * @return Result
     */
    @PostMapping("checkLoginCodeChange")
    public Result<Object> checkLoginCodeChange(@RequestParam String rid, @RequestParam String code) {
        int result = overWatchService.checkLoginCodeChange(rid, code);
        if (result == 1) {
            return ResultUtil.error("81002008", "the code is change,please login again");
        }
        return ResultUtil.success();
    }
}
